package com.yuexun.dhdoctor.module.meetingMaterial;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.email.adapter.MailFileAdapter;
import com.yuexun.dhdoctor.module.email.bean.MailFileDataBean;
import com.yuexun.dhdoctor.module.meetingMaterial.adapter.MeetingMaterialDetailAdapter;
import com.yuexun.dhdoctor.module.meetingMaterial.bean.MeetingMaterialDetailData;
import com.yuexun.dhdoctor.module.meetingMaterial.model.impl.MeetingMaterialDetailModel;
import com.yuexun.dhdoctor.module.meetingMaterial.presenter.IMeetingMaterialDetailPresenter;
import com.yuexun.dhdoctor.module.meetingMaterial.presenter.impl.MeetingMaterialDetailPresenter;
import com.yuexun.dhdoctor.module.meetingMaterial.ui.IMeetingMaterialDetailView;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Act_MeetingMaterialDetail extends BaseKJActivity implements IMeetingMaterialDetailView {

    @Bind(R.id.meeting_material_detail_title)
    TextView titleTxt;
    @Bind(R.id.noting)
    TextView notingTxt;
    @Bind(R.id.from_name_txt)
    TextView fromNameTxt;
    @Bind(R.id.meeting_material_time_txt)
    TextView tiemTxt;
    @Bind(R.id.meeting_material_detail_list)
    FixedSwipeListView listView;
    @Bind(R.id.meeting_material_feedback_record_list)
    FixedSwipeListView feedBackRecordList;
    @Bind(R.id.feedback_information_btn)
    Button feedbackInformationBtn;
    @Bind(R.id.meetingPlace)
    TextView meetingPlace;
    @Bind(R.id.meeting_content)
    TextView meetingContent;
    @Bind(R.id.meetingVisit)
    TextView meetingVisit;
    @Bind(R.id.content_view)
    ScrollView contentView;
    @Bind(R.id.permiss_txt)
    TextView permissTxt;


    private String meetingMaterialId, fromName;
    private String fileUuid[];

    private List<MeetingMaterialDetailData> mDatasList = new ArrayList<>();

    private List<MailFileDataBean> mailFileDataBeenList = new ArrayList<>();

    private MailFileAdapter adapter;


    private IMeetingMaterialDetailPresenter mMeetingMaterialDetailPresenter;

    private MeetingMaterialDetailAdapter mMeetingMaterialDetailAdapter;


    @Override
    public void setRootView() {
        setContentView(R.layout.act_meeting_material_detail);
        ButterKnife.bind(this);
        meetingMaterialId = getIntent().getStringExtra("meetingMaterialId");
//        fromName = getIntent().getStringExtra("fromName");
        mMeetingMaterialDetailPresenter = new MeetingMaterialDetailPresenter(this, new MeetingMaterialDetailModel());
    }

    @Override
    public void initData() {
        super.initData();
        mMeetingMaterialDetailPresenter.showMeetingMaterialDetail(
                PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID),
                Long.parseLong(meetingMaterialId)
        );
    }

    @Override
    public void showMeetingMaterialDetail(List<MeetingMaterialDetailData> datasList) {
        mDatasList.clear();
        mDatasList = datasList;
        if (datasList != null && datasList.size() > 0) {
            titleTxt.setText(mDatasList.get(0).getTitle());
            tiemTxt.setText(mDatasList.get(0).getCreateDate());
            fromNameTxt.setText(mDatasList.get(0).getFromName());
            meetingPlace.setText(mDatasList.get(0).getMeetingAddress());
            try {
                Date start = new SimpleDateFormat("yyyy-MM-dd").parse(mDatasList.get(0).getStartTime());
                Date end = new SimpleDateFormat("yyyy-MM-dd").parse(mDatasList.get(0).getEndTime());
                Date end2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(mDatasList.get(0).getEndTime());
                if (start.equals(end)) {
                    notingTxt.setText(mDatasList.get(0).getStartTime() + " - " + new SimpleDateFormat("HH:mm:ss").format(end2));
                } else {
                    notingTxt.setText(mDatasList.get(0).getStartTime() + " - " + new SimpleDateFormat("MM-dd HH:mm:ss").format(end2));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            /*if (mDatasList.get(0).getOutVisit() == 1) {
                meetingVisit.setText("允许");
            } else {
                meetingVisit.setText("不允许");
            }*/


            if (mDatasList.get(0).getOutVisit() == 1 && mDatasList.get(0).getInVisit() == 1) {
                meetingVisit.setText("内部人员 外部人员");
            } else {
                if (mDatasList.get(0).getOutVisit() == 1) {
                    meetingVisit.setText("外部人员");
                } else if (mDatasList.get(0).getInVisit() == 1) {
                    meetingVisit.setText("内部人员");
                } else {
                    meetingVisit.setText("不允许");
                }
            }

            meetingContent.setText(mDatasList.get(0).getContent());
            fileUuid = mDatasList.get(0).getAttachments().split(";");
            for (int i = 0; i < fileUuid.length; i++) {
                mMeetingMaterialDetailPresenter.showFileDetail(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID), fileUuid[i]);
            }
            if (datasList.get(0).getFeedbackList().size() > 0) {
                mMeetingMaterialDetailAdapter = new MeetingMaterialDetailAdapter(feedBackRecordList, datasList.get(0).getFeedbackList(), R.layout.item_feedback);
                feedBackRecordList.setAdapter(mMeetingMaterialDetailAdapter);
            }

            String[] ids = datasList.get(0).getToIds().split(";");
            if (Arrays.asList(ids).contains(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID))) {
                feedBackRecordList.setVisibility(View.VISIBLE);
                feedbackInformationBtn.setVisibility(View.VISIBLE);
            } else {
                feedBackRecordList.setVisibility(View.GONE);
                feedbackInformationBtn.setVisibility(View.GONE);
            }

            for (MeetingMaterialDetailData.FeedbackListEntity entity : datasList.get(0).getFeedbackList()) {
                if (PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID).equals(entity.getMemberId() + "")) {
                    feedbackInformationBtn.setVisibility(View.GONE);
                }
            }
        } else {
            contentView.setVisibility(View.GONE);
            permissTxt.setVisibility(View.VISIBLE);
            feedbackInformationBtn.setVisibility(View.GONE);
        }

    }

    @Override
    public void showFileDetail(List<MailFileDataBean> datasList) {
        mailFileDataBeenList = datasList;
        adapter = new MailFileAdapter(this, (ArrayList) mailFileDataBeenList, false);
        listView.setAdapter(adapter);
    }

    @OnClick({R.id.back, R.id.feedback_information_btn})
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.back:
                finish();
                break;
            case R.id.feedback_information_btn:
                Intent intent = new Intent(aty, Act_MeetingMaterialFeedBack.class);
                intent.putExtra("id", mDatasList.get(0).getMeetingMaterialId());
                aty.startActivityForResult(intent, 1);
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 100:
                mMeetingMaterialDetailPresenter.showMeetingMaterialDetail(
                        PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ACCOUNT_ID),
                        Long.parseLong(meetingMaterialId));
                break;
            default:
                break;
        }
    }
}
